<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <title>分店批发退货详情页面(修改)</title>
	<link rel="stylesheet" href="../../res/mln/layui.css" media="all">
  <style>
    body{margin: 10px;}
    .demo-carousel{height: 300px; line-height: 300px; text-align: center;}
    .demoTable{
    	z-index: 10px;
    }
    #addReload{
    	position:absolute;
    	top:0%;
    	z-index: 100px;
    	background: rgb(0,0,0,0.9);
    	width:100%;
    	height: 100%;
    	display: none;
    }
    #addOperation{
    	position: absolute;
    	left: 30%;
    	top: 0%;
    	width: 40%;
    	background: white;
    	
    	
    }
     #updateReload{
    	position:absolute;
    	top:0%;
    	z-index: 100px;
    	background: rgb(0,0,0,0.9);
    	width:100%;
    	height: 100%;
    	display: none;
    }
    #updateOperation{
    	position: absolute;
    	left: 30%;
    	top: 0%;
    	width: 40%;
    	background: white;
    	
    	
    }
    #anniu{
    	position: relative;
    	left:655px;
    	top:-30px;
    
    }
  </style>
</head>
<body>
	<div class="demoTable">
	
	 
	  <table class="layui-hide" id="wholeSaleDetail" lay-filter="demo"></table>
	</div>
	
	<!-- 文本域 -->
	<div class="text">
		<textarea id="textarea" rows="4" cols="95" placeholder="请输入退货原因"  maxlength="10" style="border:1px solid #dddddd" required></textarea>
	</div>
	
	
 	<div id="addReload">
 		
    </div>
    <div id="updateReload">
 		
   </div>
<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="salesReturn">退货</a>
</script>
<script type="text/html" id="salesReturnQuantity">
	<div class="layui-inline">
	    <input class="layui-input input" name="{{ d.pfxqlId}}" id="demoReload" autocomplete="off" style="height:30px" onkeyup="num(this);" maxlength="5" required >
		<input class="layui-input" type="hidden" id="{{ d.pfxqlId}}">
	 </div>
</script>

<script src="../../res/mln/jquery-1.10.2.js"></script>
<script src="../../res/mln/layui-xtree.js"></script>
<script src="../../res/mln/layui.js"></script>
<script src="../../res/js/base.js"></script>
<script type="text/javascript" src="../../res/mln/getUrlParam.js"></script>

<script>
var str = "";
layui.use(['form','laydate', 'laypage', 'layer', 'table', 'upload', 'element','tree'], function(){
  var form = layui.form
  ,laydate = layui.laydate //日期
  ,laypage = layui.laypage //分页
  ,layer = layui.layer //弹层
  ,table = layui.table //表格
  ,upload = layui.upload //上传
  ,element = layui.element; //元素操作
  var path=getRootPath();
  
  
  //取到销售ID
  var pfId = getUrlParam('pfId');
  //alert("pfId="+pfId);
  
  //上传文件
  var uploadInst = upload.render({
      elem: '#upload1' //绑定元素
      ,url: path+'member/uploadFile.action' //上传接口
      ,before: function(obj){
          //预读本地文件示例，不支持ie8
          obj.preview(function(index, file, result){
        	  
              $('#demoOne').attr('src', result); //图片链接（base64）
          });
      }
      ,done: function(res){
          //如果上传失败
          if(res.code > 0){
              return layer.msg('上传失败');
          }
          //上传成功
          document.getElementById("img_url").value = res.url;

      }
      ,error: function(){
          //演示失败状态，并实现重传
          var demoText = $('#demoText');
          demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
          demoText.find('.demo-reload').on('click', function(){
              uploadInst.upload();
          });
      }
  });
  //上传文件
  var uploadInsts = upload.render({
      elem: '#uploadTwo' //绑定元素
      ,url: path+'member/uploadFile.action' //上传接口
      ,before: function(obj){
          //预读本地文件示例，不支持ie8
          obj.preview(function(index, file, result){
        	  
              $('#demoTow').attr('src', result); //图片链接（base64）
          });
      }
      ,done: function(res){
          //如果上传失败
          if(res.code > 0){
              return layer.msg('上传失败');
          }
          //上传成功
          document.getElementById("img_urlTwo").value = res.url;

      }
      ,error: function(){
          //演示失败状态，并实现重传
          var demoText = $('#demoTextTwo');
          demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
          demoText.find('.demo-reload').on('click', function(){
        	  uploadInsts.upload();
          });
      }
  });
  
  //监听Tab切换
  element.on('tab(demo)', function(data){
    layer.msg('切换了：'+ this.innerHTML);
    console.log(data);
  });
  var $ = layui.$;
  //执行一个 table 实例
  table.render({
     elem: '#wholeSaleDetail'
    ,async: false
    ,url: path+'wholesaleDetail/wholesaleFindAll.action?pfId='+pfId //数据接口
    ,page: true //开启分页
    ,method:'post'
    ,size:'sm'
    ,cols: [[ //表头
       {field: 'pfxqlId', title: '销售批发详情ID', width:80 }
      ,{field: 'pfId', title: '批发ID', width:80}
      ,{field: 'pwId', title: '业务ID', width:80}
      ,{field: 'pfxqUnit', title: '单位', width:80}
      ,{field: 'pfxqNumber', title: '数量', width:80}
      ,{field: 'pfxqSubtotal', title: '小计', width:80 }
      ,{field: 'pfxqPrice', title: '批发价', width:80}
      ,{field: 'isva', title: '是否有效', width:80}
      ,{field: 'sort', title: '排序', width:80}
      ,{field: 'numbers', title: '批次号', width:100}
      ,{field: 'salesReturnQuantity', title: '退货数量', width:120,templet:'#salesReturnQuantity'}

    ]]
  	,id: 'testReload'
  	,done: function(res, curr, count){
  		str = table.cache.testReload;
  	  }
  });
  layui.code
 
  table.on('edit(demo)', function(obj){ //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
	  console.log(obj.value); //得到修改后的值
	  console.log(obj.field); //当前编辑的字段名
	  console.log(obj.data); //所在行的所有相关数据  
	  alert(obj.value);
	});
  
  //监听工具条
  table.on('tool(demo)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
    var data = obj.data //获得当前行数据
    ,layEvent = obj.event; //获得 lay-event 对应的值
    if(layEvent === 'del'){
      var reId=data.reId;
      layer.confirm('确认删除？', function(index){
        obj.del(); //删除对应行（tr）的DOM结构
        layer.close(index);
        //使用jquery的ajax方式向后台提交数据
		var url = path+"salesReturn/deleteReId.action";
		var data={reId:reId};
			$.post(url,data,function(data){
				
				if(data=="ok"){
					layer.msg('删除成功', {
						  icon: 1,
						  time: 1000 //1秒关闭（如果不配置，默认是3秒）
						}, function(){
						  
					});
				}
			});
      	});
    }
    });

		//条件查询
		/* var $ = layui.$; */
	
		
		//点击提交财务，库存做处理
		$('#anniu').on('click',function(){
			if($(".input").val() == ""){
				layer.msg("必填项不能为空");
				return;
			}
			//取到退货原因
			var reCause = $("#textarea").val();
			//取到退货总金额
			var totalMoney = $("#totalMoney").text();
			var pfxqSubtotal  = data
			
			var arr = [];
			var strs = "";
			for(var i=0;i<str.length;i++){
				var rows = "";
				if(i<(str.length-1)){
					rows = {"pfxqlId":str[i].pfxqlId,"rdNumber":$("#"+str[i].pfxqlId).val(),"drugId":str[i].drugId,"rdSubtotal":str[i].pfxqSubtotal};
				}else {
					rows = {"pfxqlId":str[i].pfxqlId,"rdNumber":$("#"+str[i].pfxqlId).val(),"drugId":str[i].drugId,"rdSubtotal":str[i].pfxqSubtotal};
				}
				arr.push(rows);
			}
			var jsonStr = JSON.stringify(arr);
			var url = path+"detail/updateIsvas.action";
		    var data={"pfId":pfId,"jsonStr":jsonStr,"reCause":reCause,"totalMoney":totalMoney};
			$.post(url,data,function(data){
				if(data=="ok"){
					layer.msg('重新填写成功', {
						  icon: 1,
						  time: 1000 //1秒关闭（如果不配置，默认是3秒）
						}, function(){
							parent.location.reload();
						  
					});
				}
				else{
					layer.msg('退货失败', {
						  icon: 1,
						  time: 1000 //1秒关闭（如果不配置，默认是3秒）
						}, function(){
						location.href='wholesale.html';
					});
				}
			});
		})
		
		$('.demoTable .layui-btn').on('click', function() {
			var demoReload = $('#demoReload');
			//执行重载
			table.reload('testReload', {
				page : {
					curr : 1
				//重新从第 1页开始
				},
				where : {
					reCause : demoReload.val()
				}
			});
		});
		$('#closeAdd').on('click', function() {
			$('#addReload').css('display', 'none');
		});
		$('#closeUpdate').on('click', function() {
			
			$('#updateReload').css('display', 'none');
		});
		
		$('#memberAdd').on('click', function() {
			$('#addReload').css('display', 'block');
			
		});
	});
	//限制只能输入数字的正则表达式
	function num(obj){
		obj.value = obj.value.replace(/[^\d]/g,"");
		$("#totalMoney").text("0");
		$("#totalNumber").text("0");
		var totalMoney = 0;
		var totalNumber = 0;
		for(var i=0;i<str.length;i++){
			if($(obj).attr("name") == str[i].pfxqlId){
				if(obj.value > str[i].pfxqNumber){
					obj.value = str[i].pfxqNumber;
				}
			}
			$("#"+$(obj).attr("name")).val(obj.value);
			//alert(str[i].pfxqNumber+obj.value);
			var numOne = $("#"+str[i].pfxqlId).val()-0;
			var numTwo = str[i].pfxqNumber-0;
			//如果退货数量小于总数量
			if(numOne <= numTwo){
				totalMoney += str[i].pfxqPrice * numOne;
				totalNumber += numOne;
			}else{
				//判断药品名称是否等于此ID
				if($(obj).attr("name") == str[i].pfxqlId){
					obj.value = str[i].pfxqNumber;
				}
				totalMoney += str[i].pfxqPrice * numTwo;
				totalNumber += str[i].pfxqNumber;
			}
		};
		$("#totalMoney").text(totalMoney);
		$("#totalNumber").text(totalNumber);
	}
	
</script>
<div><h3 style="color:#c2c2c2">总计:<span id="totalMoney">0</span>元</h3><h3 style="color:#c2c2c2" >总数量:<span id="totalNumber">0</span>件</h3><button class="layui-btn layui-btn-radius" id="anniu">立即提交</button></div>

</body>
